package module.map.propertys
{
	import drongo.desktop.selector.SelectorEvent;
	import drongo.model.core.IProperty;
	import drongo.model.propertys.NumberProperty;
	import drongo.model.propertys.StringProperty;
	import drongo.model.values.CollectionValue;
	
	import flash.events.Event;
	
	import common.view.FGUIMediator;
	
	import fairygui.GObject;
	
	import module.map.model.MapModel;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	
	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;
	
	import ui.Map.UI_NumberPropertyView;
	import ui.Map.UI_PathPropertyView;
	import ui.Map.UI_PropertyPanel;
	import ui.Map.UI_StringPropertyView;
	
	public class PropertyPanelMediator extends FGUIMediator
	{
		[Inject]
		public var view:UI_PropertyPanel;
		[Inject]
		public var project:ProjectModel;
		[Inject]
		public var mediatorMap:IMediatorMap;
		
		private var __currentMap:MapModel;
		private var currentData:CollectionValue;
		private var views:Array=[];
		private var excludes:Array;
		public function PropertyPanelMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.excludes=[
				Propertys.ID,
				Propertys.EDIATRO_MODE,
				Propertys.LOCK,
				Propertys.VISIBLE,
				Propertys.TYPE
			];
			this.AddEvents();
			this.Refresh();
		}
		
		private function set currentMap(value:MapModel):void{
			if(this.__currentMap){
				this.__currentMap.selector.removeEventListener(SelectorEvent.SELECTLIST_CHANGE,this.SelectListChanged);
			}
			this.__currentMap=value;
			if(this.__currentMap){
				this.__currentMap.selector.addEventListener(SelectorEvent.SELECTLIST_CHANGE,this.SelectListChanged);
			}
		}
		
		private function SelectListChanged(e:SelectorEvent):void{
			Refresh();
		}
		
		private function Refresh():void{
			//清除所有
			for each (var itemView:GObject in views) 
			{
				this.mediatorMap.unmediate(itemView);
			}
			this.view.m_list.removeChildren();
			
			if(!this.__currentMap){
				return;
			}
			if(this.__currentMap.selector.selectedList.length==0){
				return;
			}
			var selectList:Array=this.__currentMap.selector.selectedList;
			this.currentData=selectList[0];
			if(!this.currentData){
				return;
			}
			var itemData:IProperty;
			var dataList:Array=this.currentData.list;
			dataList.sort(compareFunction);
			for (var i:int = 0; i <dataList.length; i++) 
			{
				itemData=dataList[i];
				if(this.excludes.indexOf(itemData.GetKey())>=0){
					continue;
				}
				if(itemData is StringProperty){
					if(itemData.GetKey()==Propertys.PATH){
						itemView=UI_PathPropertyView.createInstance();
					}else{
						itemView=UI_StringPropertyView.createInstance();
					}
				}else if(itemData is NumberProperty){
					itemView=UI_NumberPropertyView.createInstance();
				}
				if(itemView){
					itemView.data={data:this.currentData,property:itemData};
					this.view.m_list.addChild(itemView);
					mediatorMap.mediate(itemView);
				}
			}
		}
		
		private function compareFunction(a:IProperty,b:IProperty):int{
			if(a.GetKey()<b.GetKey()){
				return -1;
			}else if(a.GetKey()>b.GetKey()){
				return 1;
			}
			return 0;
		}
		
		override public function destroy():void{
			super.destroy();
			this.RemoveEvents();
		}
		
		private function AddEvents():void{
			this.project.addEventListener(Event.CHANGE,this.SwitchMapHandler);
		}
		
		private function RemoveEvents():void{
			this.project.removeEventListener(Event.CHANGE,this.SwitchMapHandler);
			if(this.__currentMap){
				this.__currentMap.selector.removeEventListener(SelectorEvent.SELECTLIST_CHANGE,this.SelectListChanged);
			}
		}
		
		private function SwitchMapHandler(e:Event):void{
			this.currentMap=this.project.currentMap;
			Refresh();
		}
	}
}